﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using Willson.JsbManager.Entity;
using Willson.JsbManager.Idal;
using Willson.JsbManager.Comm;

namespace Willson.JsbManager.Dal
{
    public class DAL_table_xt_sb_jd:BaseDal<TableXtSbJdEntity>,ITableXtSbJd
    {
        string sql_sort = string.Empty;
        
        public void SetSqlSort(string text)
        {
            sql_sort = text;  
        }

        public void SetDefaultSqlSort()
        {
            sql_sort = " order by sb_year DESC,sb_jd DESC ";   
        }
       

        #region 对象实例及构造函数
        
		public static DAL_table_xt_sb_jd Instance
		{
			get
			{
				return new DAL_table_xt_sb_jd();
			}
		}
        public DAL_table_xt_sb_jd()
            : base("table_xt_sb_jd", "id", string.Empty, true)
		{
            sql_sort = " order by sb_year DESC,sb_jd DESC ";   
        }

        #endregion

        /// <summary>
        /// 将DataReader的属性值转化为实体类的属性值，返回实体类
        /// </summary>
        /// <param name="dr">有效的DataReader对象</param>
        /// <returns>实体类对象</returns>
        protected override TableXtSbJdEntity DataReaderToEntity(IDataReader dataReader)
        {
            TableXtSbJdEntity entity = new TableXtSbJdEntity();
            SmartDataReader reader = new SmartDataReader(dataReader);

            entity.Id = reader.GetInt32("id");
            entity.Company_id = reader.GetInt32("company_id");
            entity.Jd_cjh = reader.GetInt32("jd_cjh");
            entity.Jd_jh = reader.GetInt32("jd_jh");
            entity.Jd_jh_add = reader.GetInt32("jd_jh_add");
            entity.Jd_jh_begin = reader.GetInt32("jd_jh_begin");
            entity.Jd_scjh = reader.GetInt32("jd_scjh");
            entity.Jd_sum = reader.GetInt32("jd_sum");
            entity.Memo = reader.GetString("memo");
            entity.Sb_jd = reader.GetInt32("sb_jd");
            entity.Sb_year = reader.GetInt32("sb_year");


            return entity;
        }

        /// <summary>
        /// 将实体对象的属性值转化为Hashtable对应的键值(用于插入或者更新操作)
        /// (提供了默认的反射机制获取信息，为了提高性能，建议重写该函数)
        /// </summary>
        /// <param name="obj">有效的实体对象</param>
        /// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(TableXtSbJdEntity obj)
        {
            TableXtSbJdEntity info = obj as TableXtSbJdEntity;
            Hashtable hash = new Hashtable();

            hash.Add("id", info.Id);
            hash.Add("company_id", info.Company_id);
            hash.Add("jd_cjh", info.Jd_cjh);
            hash.Add("jd_jh", info.Jd_jh);
            hash.Add("jd_jh_add", info.Jd_jh_add);
            hash.Add("jd_jh_begin", info.Jd_jh_begin);
            hash.Add("jd_scjh", info.Jd_scjh);
            hash.Add("jd_sum", info.Jd_sum);
            hash.Add("memo", info.Memo);
            hash.Add("sb_jd", info.Sb_jd);
            hash.Add("sb_year", info.Sb_year);

            return hash;
        }

        #region 重写基类接口
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合
        /// </summary>
        /// <param name="recordTable">查询的条件</param>
        /// <param name="isSort">是否排序</param>
        /// <param name="op">操作方式</param>
        /// <returns>指定对象的集合</returns>
        [CLSCompliantAttribute(false)]
        public override List<TableXtSbJdEntity> Find(Hashtable recordTable, SqlOperator op, bool isSort)
        {
            SqlParameter[] param = new SqlParameter[recordTable.Count];
            IEnumerator eKeys = recordTable.Keys.GetEnumerator();

            StringBuilder tmp_where = new StringBuilder(null);
            string where = string.Empty;
            string order = string.Empty;
            string sql = string.Empty;

            int index = 0;

            while (eKeys.MoveNext())
            {
                string field = eKeys.Current.ToString();

                if (op == SqlOperator.And)
                {
                    tmp_where.Append(string.Format(" {0} = @{1} and", field, field));
                }
                else if (op == SqlOperator.Like)
                {
                    tmp_where.Append(string.Format(" {0} like '%'+@{1}+'%' and", field, field));
                }
       
                string val = recordTable[eKeys.Current.ToString()].ToString();
                param[index] = new SqlParameter(string.Format("@{0}", field), val);

                index++;

            }
            
            
            where = tmp_where.ToString().Substring(0, tmp_where.Length - 3);
    

            if (isSort)
            {
                order = sql_sort;
            }

            sql = string.Format("select {0} from {1} where {2} {3}", selectedFields, tableName, where, order);

            return GetList(sql, param);

        }

        #endregion
    }
}
